package com.example.week02.day4.Activity;

import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.week02.R;
import com.example.week02.day4.CustomView.DanmuView;

import java.util.List;
import java.util.Random;


//弹幕Activity
public class BulletChat extends AppCompatActivity {

    // 添加成员变量
    private Handler autoSendHandler = new Handler();
    private boolean isAutoSending = false;
    private final Random random = new Random();
    private static final int MAX_LENGTH = 20;
    private static final int MIN_LENGTH = 1;
    private static final int SEND_DELAY = 500; // 发送间隔(毫秒)
    //模拟高并发场景


    private DanmuView danmuView;
    private EditText etInput;
    private List<DanmuView.DanmuItem> savedDanmuState;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_bullet_chat);

        danmuView = findViewById(R.id.danmuView);
        etInput = findViewById(R.id.etInput);
        Button btnSend = findViewById(R.id.btnSend);

        btnSend.setOnClickListener(v -> sendDanmu());

        etInput.setOnEditorActionListener((v, actionId, event) -> {
            sendDanmu();
            return true;
        });
    }

    //处理屏幕旋转  更新弹幕位置适应新的屏幕尺寸
    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.d("BulletChat", "屏幕方向改变: " + newConfig.orientation);

        if (danmuView != null) {
            savedDanmuState = danmuView.saveDanmuState();
            danmuView.post(() -> danmuView.restoreDanmuState(savedDanmuState));
        }
    }

    //保存临时状态 保存弹幕状态以便重建时恢复
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        if (danmuView != null) {
            savedDanmuState = danmuView.saveDanmuState();
        }
    }

    //恢复临时状态 使用之前保存的状态恢复弹幕
    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedDanmuState != null && danmuView != null) {
            danmuView.restoreDanmuState(savedDanmuState);
        }
    }


    // 发送弹幕
    private void sendDanmu() {
        if (!isAutoSending) {
            // 开始自动发送
            isAutoSending = true;
            autoSendHandler.postDelayed(this::autoSendDanmuTask, SEND_DELAY);
            Toast.makeText(this, "开始高并发弹幕测试", Toast.LENGTH_SHORT).show();
        } else {
            // 停止自动发送
            isAutoSending = false;
            autoSendHandler.removeCallbacksAndMessages(null);
            Toast.makeText(this, "停止弹幕测试", Toast.LENGTH_SHORT).show();
        }
    }

    // 自动发送任务
    private void autoSendDanmuTask() {
        if (!isAutoSending) return;

        // 生成长度1-20的随机数字串
        int length = random.nextInt(MAX_LENGTH - MIN_LENGTH + 1) + MIN_LENGTH;
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(random.nextInt(10)); // 0-9的随机数字
        }

        // 发送弹幕
        danmuView.addDanmu(sb.toString());

        // 继续下一次发送
        autoSendHandler.postDelayed(this::autoSendDanmuTask, SEND_DELAY);
    }

    //清理资源
    @Override
    protected void onDestroy() {
        super.onDestroy();
        autoSendHandler.removeCallbacksAndMessages(null);
    }

//    //发送弹幕
//    private void sendDanmu() {
//        //获取EditText内容
//        String text = etInput.getText().toString().trim();
//        if (!text.isEmpty()) {
//            //调用DanmuView中的addDanmu方法
//            danmuView.addDanmu(text);
//            //清空EditText内容
//            etInput.setText("");
//        }
//    }

}